import MySQLdb
import datetime
try:
    conn = MySQLdb.Connect(host="localhost",port=3306,db="demo",user="root",passwd="",charset="utf8")
    cmd = conn.cursor()

    def goods_m_menu():
        cmd.execute("select username from nowuser")
        un = cmd.fetchall()
        print("欢迎使用丰翼超市管理系统   你好：%s" % un)
        print("*" * 20)
        print("1.添加商品")
        print("2.删除商品")
        print("3.显示所有商品")
        print("4.返回上一级")
        print("*" * 20)

    def goods_m():
        goods_m_menu()
        while True:
            num: str = input("请输入您的选择：")
            if not num.isdigit():
                print("请输入数字！")
                continue
            num: int = int(num)

            if num == 1:
                goods_add()
            elif num == 2:
                goods_delete()
            elif num == 3:
                goods_show()
            elif num == 4:
                print_menu()
            else:
                print("请按号输入！")
            print()

    def goods_add():
        name = input("商品名称：")
        number = input("商品数量：")
        price = input("商品单价：")
        cmd.execute("insert into goods values ('%s','%s','%s')" % (name, number, price))
        conn.commit()
        result: str = input("添加成功！按q返回：")
        if result == 'q':
            goods_m()

    def goods_delete():
        del_goods = input("请输入要删除的商品的名称：")
        num = cmd.execute("select price from goods where name ='%s' " % (del_goods))
        if (num >= 1):
            result1: str = input("确定删除请按y键：")
            if result1 == 'y':
                cmd.execute("delete from goods where name = %s ", (del_goods,))
                conn.commit()
                result2: str = input("删除成功！按q返回：")
                if result2 == 'q':
                    goods_m()
        else:
            print("没有找到对应的商品，请重新输入！")
            goods_delete()

    def goods_show():
        cmd.execute("select * from goods")
        rows = cmd.fetchall()
        for r in rows:
            print("%s   \t    库存%s个   \t   单价%s   \t   总计%s" % (r[0],r[1],r[2],r[1]*r[2]))

    def vip_m_menu():
        cmd.execute("select username from nowuser")
        un = cmd.fetchall()
        print("欢迎使用丰翼超市管理系统   你好：%s" % un)
        print("*" * 20)
        print("1.添加会员")
        print("2.删除会员")
        print("3.显示所有会员")
        print("4.返回上一级")
        print("*" * 20)

    def vip_m():
        vip_m_menu()
        while True:
            num: str = input("请输入您的选择：")
            if not num.isdigit():
                print("请输入数字！")
                continue
            num: int = int(num)
            if num == 1:
                vip_add()
            elif num == 2:
                vip_delete()
            elif num == 3:
                vip_show()
            elif num == 4:
                print_menu()
            else:
                print("请按号输入！")
            print()

    def vip_add():
        cmd.execute("SELECT COUNT(*) FROM vips")
        len: int = cmd.fetchone()
        today = datetime.datetime.today()
        data = today.strftime("%Y%m%d%H%M")
        number = str(data + str("%05d" % (len[0] + 1)))
        name = input("会员姓名：")
        source = input("会员积分：")
        tele = input("会员电话：")
        cmd.execute("insert into vips values ('%s','%s','%s','%s')" % (number, name, source, tele,))
        conn.commit()
        result: str = input("添加成功！按q返回：")
        if result == 'q':
            vip_m()

    def vip_delete():
        del_vip = input("请输入要删除的会员编号：")
        num = cmd.execute("select * from vips where number ='%s' " % (del_vip))
        if (num >= 1):
            result1: str = input("确定删除请按y键：")
            if result1 == 'y':
                cmd.execute("delete from vips where number = %s ", (del_vip,))
                conn.commit()
                result2: str = input("删除成功！按q返回：")
                if result2 == 'q':
                    vip_m()
        else:
            print("没有找到对应的会员编号，请重新输入！")
            vip_delete()

    def vip_show():
        cmd.execute("select * from vips")
        rows = cmd.fetchall()
        print("编号\t               姓名\t 积分\t   电话")
        for r in rows:
            print("%s\t%s\t  %s\t   %s" % (r[0], r[1], r[2], r[3]))

    def sum_price():
        a = 0
        name = input("请选择你要购买的商品名称：")
        num = cmd.execute("select * from goods where name = '%s'" % (name))
        rows = cmd.fetchall()
        if (num >= 1):
            number = int(input("请选择你要购买的商品数量："))
            for r in rows:
                if (number <= r[1]):
                    sum = number * r[2]
                    cmd.execute("insert into nowgoods values ('%s','%s','%s','%s')" % (r[0], number, r[2], sum,))
                    conn.commit()
                    result = input("确定购买该商品请按y，退出购买请按q")
                    if result == 'y':
                        n = r[1] - number
                        cmd.execute("update goods set number = '%s' where name = '%s'" % (n, name))
                        conn.commit()
                        result2 = input("继续购买请按y，结账请按q")
                        if result2 == 'y':
                            sum_price()
                        elif result2 == 'q':
                            cmd.execute("select * from nowgoods")
                            rows2 = cmd.fetchall()
                            print("商品名称\t  数量\t  单价\t  应收")
                            for r2 in rows2:
                                print("%s \t    %s\t%s\t%s" % (r2[0], r2[1], r2[2], r2[3]))
                                a = a + r2[3]
                            print("=" * 13)
                            print("合计：    %d" % a)
                            cmd.execute("delete from nowgoods")
                            conn.commit()
                            print("购买成功")
                    elif result == 'q':
                        cmd.execute("delete from nowgoods")
                        conn.commit()
                        print_menu()
                else:
                    print("库存不足，请重新购买")
                    sum_price()
        else:
            print("未找到你要购买的商品，请重新选择")
            sum_price()


    def print_menu():
        """登陆后菜单"""
        cmd.execute("select username from nowuser")
        un = cmd.fetchall()
        print("欢迎使用丰翼超市管理系统   你好：%s" % un)
        print("*" * 20)
        print("1.商品管理")
        print("2.会员管理")
        print("3.结账")
        print("4.退出系统")
        print("*" * 20)
        while True:
            num: str = input("请输入您的选择：")
            if not num.isdigit():
                print("请输入数字！")
                continue
            num: int = int(num)
            if num == 1:
                goods_m()
            elif num == 2:
                vip_m()
            elif num == 3:
                sum_price()
            elif num == 4:
                exout()
                break
            else:
                print("请按号输入！")
            print()

    def print_login():
        """登录功能打印"""
        print("欢迎使用丰翼超市管理系统")
        print("*" * 20)
        print("1.登录系统")
        print("2.退出系统")
        print("*" * 20)

    def login():
        num2 = cmd.execute("select username from nowuser")
        if(num2 >= 1):
            un = cmd.fetchall()
            cmd.execute("delete from nowuser where username = %s ", (un))
            conn.commit()
        name = input("请输入用户名:")
        password = input("请输入密码:")
        num = cmd.execute("select * from users where username ='%s' and password='%s'" % (name, password))
        if (num >= 1):
            print("登陆成功")
            cmd.execute("insert into nowuser values ('%s','%s')" % (name, password))
            conn.commit()
            print_menu()
        else:
            print("用户名或密码错误,请重新登录")
            login()

    def exout():
        num = cmd.execute("select username from nowuser")
        if(num >= 1):
            un = cmd.fetchall()
            num1 = cmd.execute("delete from nowuser where username = %s ", (un))
            conn.commit()
            if (num1 >= 1):
                print("退出成功")
                main()
            else:
                print("退出失败")
        else:
            print("您已退出，请登录")
            main()
    def main():
        print_login()
        while True:
            # 获取用户的选择
            num: str = input("请输入您的选择：")
            # 判断输入的是否为数字
            if not num.isdigit():
                print("请输入数字！")
                continue
                # 转换成数字类型
            num: int = int(num)

            if num == 1:
                login()
            elif num == 2:
                exout()
                break
            else:
                print("请按号输入！")
            print()

    if __name__ == '__main__':
        main()

except Exception as e:
    print(e)

finally:
    conn.close()
    cmd.close()